/**********************************************************************************
Copyright (C), by AppleCai
Project                      : Study Kernel driver
Description                  : User Application for cdev
History                      :
CPU and Compiler             : AM335x,GCC
GenDate                      : 2020-10-25
OwnerBy                      : Apple Cai
**********************************************************************************/
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <sys/ioctl.h>
#include "applepaper_cmd.h"

#define TEST_EACH_CMD  0
#define FILE_LOCK  0 /* 打开file open/close等级的锁 */
static char usrdata[] = {"Apple's usr data!"};

/*
 * @description		: main主程序
 * @param - argc 	: argv数组元素个数
 * @param - argv 	: 具体参数
 * @return 			: 0:成功;others:失败
 */
int main(int argc, char *argv[])
{
	int fd, retvalue;
	char *filename;
	char readbuf[100], writebuf[100];

#if TEST_EACH_CMD
	if(argc < 3){
		printf("Error Usage!Please set filename and control flag\r\n");
		return -1;
	}
#endif
	filename = argv[1];

	/* 打开驱动文件 */
	fd  = open(filename, O_RDWR);
	if(fd < 0){
		printf("Can't open file %s\r\n", filename);
		return -1;
	}
#if TEST_EACH_CMD  /* test each cmd */
	if(atoi(argv[2]) == 1){ /* 从驱动文件读取数据 */
		retvalue = read(fd, readbuf, 18);
		if(retvalue < 0){
			printf("read file %s failed!\r\n", filename);
		}else{
			/*  读取成功，打印出读取成功的数据 */
			printf("read data:%s\r\n",readbuf);
		}
	}

	if(atoi(argv[2]) == 2){
 	/* 向设备驱动写数据 */
		memcpy(writebuf, usrdata, sizeof(usrdata));
		retvalue = write(fd, writebuf, 18);
		if(retvalue < 0){
			printf("write file %s failed!\r\n", filename);
		}
	}
	
	if(atoi(argv[2]) == 3){
 	/* 向设备驱动清空数据 */
		retvalue = ioctl(fd, APPLEPAPER_CLEAR, 0);
		if(retvalue < 0){
			printf("clear file %s failed!\r\n", filename);
		}
	}
	
	if(atoi(argv[2]) == 4){
	    int pos_cmd =  atoi(argv[3]);    /* 从哪个位置开始移动 */
		long move_num =  atoi(argv[4]);  /* 移动的方向及数量 */
 	/* 向设备驱动请求移动光标位置 */
		retvalue = lseek(fd, move_num, pos_cmd);
		if(retvalue < 0){
			printf("move pos %s failed!\r\n", filename);
		}
	}
#endif
	    printf("goto file %s,line %d\n",__FILE__,__LINE__);
	    printf("start to write usrdata:%s\n",usrdata);
	 	/* 向设备驱动清空数据 */
		retvalue = ioctl(fd, APPLEPAPER_CLEAR, 0);
		if(retvalue < 0){
			printf("clear file %s failed!\r\n", filename);
		}

		memcpy(writebuf, usrdata, sizeof(usrdata));
		retvalue = write(fd, writebuf, 18);
		if(retvalue < 0){
			printf("write file %s failed!\r\n", filename);
		}

		//retvalue = lseek(fd, -6, APPLEPAPER_CUR_POS);
		retvalue = lseek(fd, 8, APPLEPAPER_BEGIN_POS);
		if(retvalue < 0){
			printf("move pos %s failed!\r\n", filename);
		}

		retvalue = read(fd, readbuf, 18);
		if(retvalue < 0){
			printf("read file %s failed!\r\n", filename);
		}else{
			/*  读取成功，打印出读取成功的数据 */
			printf("In the end,read data:%s\r\n",readbuf);
		}
		#if FILE_LOCK
	int counter = 0;
    do
	{
	    sleep(5);
		counter++;
		printf("App running times:%d\n", counter);
	}while(counter<=5);
	#endif
	/* 关闭设备 */
	retvalue = close(fd);
	if(retvalue < 0){
		printf("Can't close file %s\r\n", filename);
		return -1;
	}

	return 0;
}



